package com.xd.common.util;


import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class SendMessageUtil {
//    阿里云
    public static String sendALMsg(String signatureName, String accessKeyId, String accessKeySecret, String modelCode, String phone, Map<String, Object> param) {
        String code = "";
        try {
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setSignName(signatureName)//申请阿里云 签名名称
                    .setTemplateCode(modelCode)//申请阿里云 模板code
                    .setPhoneNumbers(phone)//手机号
                    .setTemplateParam(JSONObject.toJSONString(param));//验证码数据，转换json数据传递
            Config config = new Config()
                    .setAccessKeyId(accessKeyId)
                    .setAccessKeySecret(accessKeySecret)
                    .setEndpoint("dysmsapi.aliyuncs.com");// 访问的域名
            //请求中遇到网络问题的情况会进行重试,默认最多重试3次
            com.aliyun.dysmsapi20170525.models.SendSmsResponse sendSmsResponse = new Client(config).sendSmsWithOptions(sendSmsRequest, new RuntimeOptions().setAutoretry(true));
            code = sendSmsResponse.getBody().getCode();
            return code;
        } catch (TeaException error) {
            // 如有需要，请打印 error
            Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            Common.assertAsString(error.message);
        }
        return code;
    }

    //腾讯云发送短信
    public static String sendTXMsg(String SmsSdkAppId,String SignName, String SecretKey, String SecretId, String TemplateId, String[] phone, String[] param) {
        String code = "";
        try{
            Credential cred = new Credential(SecretId, SecretKey);
            SmsClient smsClient = new SmsClient(cred, "ap-guangzhou");
            com.tencentcloudapi.sms.v20210111.models.SendSmsRequest req = new com.tencentcloudapi.sms.v20210111.models.SendSmsRequest();
            String[] phoneNumberSet1 = phone;
            req.setPhoneNumberSet(phoneNumberSet1);
            req.setSmsSdkAppId(SmsSdkAppId);
            req.setSignName(SignName);
            req.setTemplateId(TemplateId);
            String[] templateParamSet1 = param;
            req.setTemplateParamSet(templateParamSet1);
            SendSmsResponse resp = smsClient.SendSms(req);
            SendStatus[] sendStatusSet = resp.getSendStatusSet();
            code = sendStatusSet[0].getCode();//状态码/code是 'OK'代表成功
            log.info(SendSmsResponse.toJsonString(resp));
            return code;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return code;
    }

/*    public static void main(String [] args) {
        String[] s ={"15555155944"};
        String[] a ={"啊","啊","啊","啊"};
        String[] c ={"1234"};
        SendMessageUtil.sendTXMsg("1400815847","星空欲梦","PaXFiwwiIk3CE2g8OQDm9JH2c14zmQwa","AKIDUwQm01oSwGLoCRYbdU7gCWfbcWCdqpb8","1785501",s,a);
//        SendMessageUtil.sendTXMsg("1400815847","星空欲梦","PaXFiwwiIk3CE2g8OQDm9JH2c14zmQwa","AKIDUwQm01oSwGLoCRYbdU7gCWfbcWCdqpb8","1785498",s,c);
    }*/
}
